//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "BespokeCalendar.h"
using namespace Cephei::QL::Times::Calendars;
#include <gen/QL/Times/Period.h>
#include <gen/QL/Times/Calendar.h>
using namespace Cephei::QL::Times;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Times::Calendars::CBespokeCalendar::CBespokeCalendar (Microsoft::FSharp::Core::FSharpOption<String^>^ name) : CCalendar(CBespokeCalendar::typeid)
{
    try
    {
#ifdef HANDLE
        _phBespokeCalendar = NULL;
#endif
        std::string _name = 
            (Microsoft::FSharp::Core::FSharpOption<String^>::IsSome::get (name) ? (std::string)ValueHelper::Convert (name->Value) : ""); //4
        _ppBespokeCalendar = new boost::shared_ptr<QuantLib::BespokeCalendar> (new QuantLib::BespokeCalendar ( _name ));
        SetCalendar (boost::dynamic_pointer_cast<QuantLib::Calendar> (*_ppBespokeCalendar));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Times::Calendars::CBespokeCalendar::CBespokeCalendar (boost::shared_ptr<QuantLib::BespokeCalendar>& childNative, Object^ owner) : CCalendar(CBespokeCalendar::typeid)
{
#ifdef HANDLE
	_phBespokeCalendar = NULL;
#endif
	_ppBespokeCalendar = &childNative;
    _ppCalendar = new boost::shared_ptr<QuantLib::Calendar> (boost::dynamic_pointer_cast<QuantLib::Calendar> (*_ppBespokeCalendar));
}
Cephei::QL::Times::Calendars::CBespokeCalendar::CBespokeCalendar (QuantLib::BespokeCalendar& childNative, Object^ owner) : CCalendar(CBespokeCalendar::typeid)
{
#ifdef HANDLE
	_phBespokeCalendar = NULL;
#endif
	_ppBespokeCalendar = new boost::shared_ptr<QuantLib::BespokeCalendar> (&childNative);
    _ppCalendar = new boost::shared_ptr<QuantLib::Calendar> (boost::dynamic_pointer_cast<QuantLib::Calendar> (*_ppBespokeCalendar));
    _BespokeCalendarOwner = owner;
    _CalendarOwner = owner;
}

Cephei::QL::Times::Calendars::CBespokeCalendar::CBespokeCalendar (CBespokeCalendar^ copy) : CCalendar(CBespokeCalendar::typeid)
{
#ifdef HANDLE
	_phBespokeCalendar = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppBespokeCalendar = new boost::shared_ptr<QuantLib::BespokeCalendar> (copy->GetShared());
        _ppCalendar = new boost::shared_ptr<QuantLib::Calendar> (boost::dynamic_pointer_cast<QuantLib::Calendar> (*_ppBespokeCalendar));
    }
}
Cephei::QL::Times::Calendars::CBespokeCalendar::CBespokeCalendar (System::Type^ t) : CCalendar(CBespokeCalendar::typeid)
{
#ifdef HANDLE
	_phBespokeCalendar = NULL;
#endif
	if (!t->IsSubclassOf(CBespokeCalendar::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Times::Calendars::CBespokeCalendar::CBespokeCalendar (QuantLib::Handle<QuantLib::BespokeCalendar>& childNative, Object^ owner)  : CCalendar(CBespokeCalendar::typeid)
{
	_phBespokeCalendar = &childNative;
	_ppBespokeCalendar = &static_cast<boost::shared_ptr<QuantLib::BespokeCalendar>>(childNative.currentLink());
    _ppCalendar = new boost::shared_ptr<QuantLib::Calendar> (boost::dynamic_pointer_cast<QuantLib::Calendar> (*_ppBespokeCalendar));
    _BespokeCalendarOwner = owner;
}
Cephei::QL::Times::Calendars::CBespokeCalendar::CBespokeCalendar (QuantLib::Handle<QuantLib::BespokeCalendar> childNative)  : CCalendar(CBespokeCalendar::typeid)
{
	_phBespokeCalendar = &childNative;
	_ppBespokeCalendar = &static_cast<boost::shared_ptr<QuantLib::BespokeCalendar>>(childNative.currentLink());
    _ppCalendar = new boost::shared_ptr<QuantLib::Calendar> (boost::dynamic_pointer_cast<QuantLib::Calendar> (*_ppBespokeCalendar));
}
#endif
#ifdef STRUCT
Cephei::QL::Times::Calendars::CBespokeCalendar::CBespokeCalendar (QuantLib::BespokeCalendar childNative)  : CCalendar(CBespokeCalendar::typeid)
{
#ifdef HANDLE
	_phBespokeCalendar = NULL;
#endif
	_ppBespokeCalendar = new boost::shared_ptr<QuantLib::BespokeCalendar> (new QuantLib::BespokeCalendar (childNative));
    _ppCalendar = new boost::shared_ptr<QuantLib::Calendar> (boost::dynamic_pointer_cast<QuantLib::Calendar> (*_ppBespokeCalendar));
}
#endif

Cephei::QL::Times::Calendars::CBespokeCalendar::~CBespokeCalendar ()
{
    if (_ppBespokeCalendar != NULL)
    {
	    delete _ppBespokeCalendar;
        _ppBespokeCalendar = NULL;
    }
}
Cephei::QL::Times::Calendars::CBespokeCalendar::!CBespokeCalendar ()
{
    if (_ppBespokeCalendar != NULL)
    {
	    delete _ppBespokeCalendar;
    }
}
QuantLib::BespokeCalendar& Cephei::QL::Times::Calendars::CBespokeCalendar::GetReference ()
{
    if (_ppBespokeCalendar == NULL) throw gcnew NativeNullException ();
	return **_ppBespokeCalendar;
}
boost::shared_ptr<QuantLib::BespokeCalendar>& Cephei::QL::Times::Calendars::CBespokeCalendar::GetShared ()
{
    if (_ppBespokeCalendar == NULL) throw gcnew NativeNullException ();
	return *_ppBespokeCalendar;
}
QuantLib::BespokeCalendar* Cephei::QL::Times::Calendars::CBespokeCalendar::GetPointer ()
{
    if (_ppBespokeCalendar == NULL) throw gcnew NativeNullException ();
	return &**_ppBespokeCalendar;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::BespokeCalendar>& Cephei::QL::Times::Calendars::CBespokeCalendar::GetHandle ()
{
	if (_phBespokeCalendar == NULL)
	{
		_phBespokeCalendar = new Handle<QuantLib::BespokeCalendar> (*_ppBespokeCalendar);
	}
	return *_phBespokeCalendar;
}
#endif
bool Cephei::QL::Times::Calendars::CBespokeCalendar::HasNative () 
{
	return (_ppBespokeCalendar != NULL);
}

Cephei::QL::Times::Calendars::IBespokeCalendar^ Cephei::QL::Times::Calendars::CBespokeCalendar::AddWeekend (QL::Times::WeekdayEnum w)
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        QuantLib::Weekday _w = (QuantLib::Weekday)w ;
    	(*_ppBespokeCalendar)->addWeekend ( _w );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Times::Calendars::IBespokeCalendar^ Cephei::QL::Times::Calendars::CBespokeCalendar_Factory::Create (Microsoft::FSharp::Core::FSharpOption<String^>^ name)
{
    return gcnew CBespokeCalendar ( name);
}
